#include "serverFucs.h"

bool judgeUserPwd(char* recvBuf,char* uname)
{
    int alreadReadLen = 0;
    char uName[50] = {0}; char pwd[50] = {0};
    int uNameLen; int pwdLen;

    // 读用户名
    memcpy(&uNameLen, recvBuf + alreadReadLen, 4); // 读取前4个字节
    alreadReadLen += 4;
    // printf("uNameLen: %d\n",uNameLen);
    memcpy(uName,recvBuf + alreadReadLen, uNameLen);
    memcpy(uname,recvBuf + alreadReadLen, uNameLen);
    // printf("uName:%s\n",uName);
    alreadReadLen += uNameLen;

    // 读密码
    memcpy(&pwdLen, recvBuf + alreadReadLen, 4); // 读取前4个字节
    alreadReadLen += 4;
    // printf("pwdLen: %d\n",pwdLen);
    memcpy(pwd,recvBuf + alreadReadLen, pwdLen);
    alreadReadLen += pwdLen;
    // printf("pwd:%s\n",pwd);

    // 下面代码是读取配置文件，判断账号密码是否正确，以后用数据库判断
    // TODO:
    return true;
}


// 判断文件夹是否存在
int folder_exists(const char *folder_name)
{
    struct stat st;
    if (stat(folder_name, &st) == 0)
    {
        if (S_ISDIR(st.st_mode))
        {
            return 1; // 文件夹存在
        }
        else
        {
            return 0; // 存在同名文件
        }
    }
    return 0; // 文件夹不存在
}

int creat_folder(char *folder_name)
{
    if (!folder_exists(folder_name))
    {
        // 文件不存在,创建文件
        int status = mkdir(folder_name, 0777);
        // printf("absPath:%s\n",folder_name);
        if (status == 0)
        {
            printf("文件创建成功...\n");
            return 1;
        }
        else
        {
            // printf("41创建失败...\n");
            return 0;
        }
    }
    // printf("45创建失败...\n");
    return 0;
}